package topin.facecat.FaceCat.Service

import java.util.Locale

class SecurityService {
    var m_securities = ArrayList<Security>()

    var m_securitiesMap = HashMap<String, Security>()

    /*
     * 获取是否已加载
     */
    var isLoaded = false
        private set

    /*
     * 根据代码获取证券
     *@param code 证券代码
     *@param security 证券信息
     */
    fun getSecurityByCode(code: String, security: Security): Boolean {
        if (!isLoaded) {
            return false
        }
        var key = code.uppercase(Locale.getDefault())
        key = key.replace("\r", "")
        key = key.replace("\n", "")
        return if (m_securitiesMap.containsKey(code)) {
            val copySecurity = m_securitiesMap[code]
            security.m_code = copySecurity!!.m_code
            security.m_name = copySecurity.m_name
            security.m_type = copySecurity.m_type
            security.m_block = copySecurity.m_block
            security.m_pingyin = copySecurity.m_pingyin
            true
        } else {
            false
        }
    }

    /*
     * 加载证券
     */
    fun load(securitiesCache: ArrayList<Security>) {
        m_securities.clear()
        m_securitiesMap.clear()
        val securitiesSize = securitiesCache.size
        for (i in 0 until securitiesSize) {
            val security = securitiesCache[i]
            m_securitiesMap[security.m_code.uppercase(Locale.getDefault())] = security
            m_securities.add(security)
        }
        securitiesCache.clear()
        isLoaded = true
    }
}

